﻿using System.Windows.Forms;
using Needle.Framework;
using System.Linq;
using Needle.DevToolkit.Services;
using Needle.Utils.Extensions;
using Needle.DevToolkit.Workspace;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections.Generic;
using Needle.DevToolkit.Services.UI;
using Needle.DevToolkit.UI.Controls;
using System;
using Needle.DevToolkit.UI.ToolWindows;
using System.ComponentModel.Design;
using System.Text;
using Needle.DevToolkit.Traits;
using System.Threading;

namespace Needle.DevToolkit.UI
{
    public partial class ShellForm : Form
    {

        private IServiceProvider _serviceProvider;


        public ShellForm()
        {
            InitializeComponent();
        }

        private void ShellForm_Load(object sender, System.EventArgs e)
        {

            _serviceProvider = this.GetService(typeof(IServiceProvider)) as IServiceProvider;
            IServiceContainer services = _serviceProvider.GetService<IServiceContainer>();

            var uiService = new UIService(_serviceProvider, this);
            uiService.Initialize();
            
            services.AddService<IStatusUI>(statusUIManager1);

            IComponentLoader loader = _serviceProvider.GetService<IComponentLoader>();

            var wsctrl = loader.LoadComponent<WorkspaceController>();
            var explorer = loader.LoadComponent<WorkSpaceExplorer>();
            dockPanel1.ContentAdded += new EventHandler<DockContentEventArgs>(dockPanel1_ContentAdded);
            dockPanel1.ContentRemoved += new EventHandler<DockContentEventArgs>(dockPanel1_ContentRemoved);
            explorer.Controller = wsctrl;
            explorer.HideOnClose = true;
            explorer.Show(dockPanel1, DockState.DockLeft);

            ICommandService menuService = _serviceProvider.GetService<ICommandService>();

            var menuItems = (from verb in menuService.GetCommands(new Guid(MenuGroup.File))
                             select new ItemVerbToolStripMenuItem(verb)).ToArray();

            fileToolStripMenuItem.DropDownItems.AddRange(menuItems);

            _serviceProvider
                .GetService<ISelectionService>()
                .SelectionChanged += OnSelectionChanged;

        }

        void dockPanel1_ContentRemoved(object sender, DockContentEventArgs e)
        {
            
        }

        void dockPanel1_ContentAdded(object sender, DockContentEventArgs e)
        {
            
        }

        private object _lastStatus;
        private void OnSelectionChanged(object sender, EventArgs e)
        {
            var statusUI = _serviceProvider.GetService<IStatusUI>();
            if (_lastStatus != null)
                statusUI.RemoveStatus(_lastStatus);
            var ss = sender as ISelectionService;
            if (ss.SelectionCount > 0)
            {
                string listSeparator = Thread.CurrentThread.CurrentCulture.TextInfo.ListSeparator;
                StringBuilder items = new StringBuilder();
                foreach (object item in ss.GetSelectedComponents())
                {
                    if (item is IName)
                        items.Append(((IName)item).Name);
                    else
                        items.Append(item.GetType().Name);
                    items.Append(listSeparator);
                }
                if (items.Length > listSeparator.Length)
                    items.Length -= listSeparator.Length;
                _lastStatus = statusUI.SetStatus(items.ToString(), 0);
            }
        }
    }
}
